package com.undsf.ogd;

import java.util.ArrayList;
import java.util.Set;
import java.util.List;

/**
 * Created by Arathi on 2015/10/19.
 */
public class Company {
    protected String name; //公司名称
    protected int money; //资金
    protected int maxEmployeeAmount; //最大雇员数量
    protected List<Peopleware> employees; //雇员表
    protected Game developingGame;
    protected int level; //公司规模
    protected Set<Integer> enabledGameType;
    protected Set<Integer> enabledGameTheme;
    protected List<Game> works;

    public Company(){
        init("UND软件基金会");
    }

    public void init(String name)
    {
        System.out.println("开始初始化新公司\""+name+"\"");
        //设置公司名称
        this.name = name;
        //设置初始资金
        this.money = 1000;
        //设置最大雇员人数
        this.level = 0;
        this.maxEmployeeAmount = 4;
        employees = new ArrayList<>();
        for (int i=0; i<maxEmployeeAmount; i++) {
            employees.add(null);
        }
        //分配初始员工
        Society.WorkDistribution(this, 1);
        Society.WorkDistribution(this, 2);
        Society.RegisterCompany(this);
        System.out.println("新公司\"" + name + "\"创建完成！");
    }

    public List<OutSource> getOutsourceProjectList(){
        return null;
    }

    public String getName(){
        return name;
    }

    public boolean addEmployee(Peopleware people) {
        //添加到数组中
        Company company = people.getEmployer();
        if (company != null){
            System.err.println(people.getName() + "已经受雇于" + company.getName());
            return false;
        }
        int emptyIndex = getFirstEmptyIndex();
        if (emptyIndex < 0 || emptyIndex >= maxEmployeeAmount){
            System.err.println( getName() + "没有更多的岗位，" + people.getName() + "暂时无法加入！");
            return false;
        }
        people.setEmployer(this);
        employees.set(emptyIndex, people);
        return true;
    }

    /**
     * 获取策划人员
     */
    public List<Peopleware> getAllPlaners(){
        List<Peopleware> planers = new ArrayList<>();
        for (Peopleware planer : employees){
            if (planer != null && planer.getSkillLevel(Peopleware.JobIdWriter) > 0){
                planers.add(planer);
            }
        }
        List<Peopleware> freePlaners = Society.GetFreePlaners(level);
        planers.addAll(freePlaners);
        return planers;
    }

    /**
     * 获取画师
     */
    public List<Peopleware> getAllDesigners(){
        List<Peopleware> designers = new ArrayList<>();
        for (Peopleware designer : employees){
            if (designer != null && designer.getSkillLevel(Peopleware.JobIdDesigner) > 0){
                designers.add(designer);
            }
        }
        List<Peopleware> freeDesigner = Society.GetFreeDesigners(level);
        designers.addAll(freeDesigner);
        return designers;
    }

    /**
     * 获取音乐制作人
     */
    public List<Peopleware> getAllSoundEngs(){
        List<Peopleware> soundEngs = new ArrayList<>();
        for (Peopleware soundEng : employees){
            if (soundEng != null && soundEng.getSkillLevel(Peopleware.JobIdSndEng) > 0){
                soundEngs.add(soundEng);
            }
        }
        List<Peopleware> freeSoundEng = Society.GetFreeSoundengs(level);
        soundEngs.addAll(freeSoundEng);
        return soundEngs;
    }

    /**
     * 获取员工数量
     * @return
     */
    public int getEmployeeAmount() {
        int amount = 0;
        for (int index = 0; index < employees.size(); index++){
            if (employees.get(index) != null) amount++;
        }
        return amount;
    }

    /**
     * 获取第一个空的员工索引
     * @return 返回空位索引；如果没有找到空位的索引，返回-1
     */
    public int getFirstEmptyIndex() {
        for (int index=0; index<employees.size(); index++){
            if (employees.get(index) == null) return index;
        }
        return -1;
    }

}
